<?php
 require_once('menu.php');
 if ($offerId) {
    $url = "/{$offerId}";
 } else {
    $url = "";
 }
 ?>
<script type="text/javascript">
$().ready(function() {
    $("#businessForm").validate({
        errorLabelContainer: $("#businessForm div.error")
    });
});
</script>

<!-- TinyMCE -->
<?php echo $javascript->link('tiny_mce/tiny_mce.js'); ?>
<script type="text/javascript">
	//tinyMCE.init({
	//	mode : "textareas",
	//	theme : "simple"
	//});

    tinyMCE.init({
    	// General options
    	mode : "textareas",
    	theme : "advanced",
    	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",

    	// Theme options
    	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
    	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code",
    	theme_advanced_buttons3 : "insertdate,inserttime,preview,|,forecolor,backcolor",
    	theme_advanced_toolbar_location : "top",
    	theme_advanced_toolbar_align : "left",
    	theme_advanced_statusbar_location : "bottom",
    	theme_advanced_resizing : true,

    	// Example content CSS (should be your site CSS)
    	content_css : "css/example.css",

    	// Drop lists for link/image/media/template dialogs
    	template_external_list_url : "js/template_list.js",
    	external_link_list_url : "js/link_list.js",
    	external_image_list_url : "js/image_list.js",
    	media_external_list_url : "js/media_list.js",

    	// Replace values for the template plugin
    	template_replace_values : {
    		username : "Some User",
    		staffid : "991234"
    	}
    });
</script>
<!-- /TinyMCE -->

<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%">
        <?php
         if (isset($alert)) {
         ?>
            <div class="pathway">Alert:&nbsp;<?=$alert?></div>
        <?php
         }
         ?>
        </td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="javascript: document.getElementById('flag').value='offer'; submitthis();">
                        <?php echo $html->image('admin/mediamanager_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Offers
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" href="#">
                        <?php echo $html->image('admin/help_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">
        <table class="adminheading">
            <tr>
                <th class="info" rowspan="2" nowrap>Add Offers</th>
            </tr>
        </table>


        <table class="adminform">
            <tr>
                <td width="60%" valign="top">
                    <form id="businessForm" name="businessForm" method="post" action="<?=$html->url('/admin/add_offers'.$url)?>" enctype="multipart/form-data">
                    <div class="error"></div>
                    <table class="adminlist" cellpadding=20>
                    	<tr>
                    		<th>Offer Details</th>
                    	</tr>
                    	<tr>
                    		<td align="left"></td>
                    	</tr>
                    </table>

                    <table width="100%">
                        <tr>
                            <td width="60%">
                                <table class="adminlist" cellpadding=20>
                                    <tr>
                                        <td align="left" width="20%"><font color="red"><b>*</b></font>&nbsp;Networks</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                $network_val = $offers['Offer']['ad_network'];
                                             } else {
                                                $network_val = "";
                                             }
                                             echo $form->select('ad_network', $network, $network_val, array('id' => 'network', 'style' => 'width: 200px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left"><font color="red"><b>*</b></font>&nbsp;Offer name</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][offer_name]" id="offer_name" style="width: 400px" value="<?php echo ($offers) ? $offers['Offer']['offer_name'] : ""; ?>" title="<br/>" class="{required:true}">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Offer Link-out URL</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][offer_url]" id="field_length" title="" class="" style="width: 400px" value="<?php echo ($offers) ? $offers['Offer']['offer_url'] : ""; ?>">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Post Method</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $pmethod = $offers['Offer']['post_method'];
                                             } else {
                                                $pmethod = null;
                                             }
                                             echo $form->select('post_method', $post_method, $pmethod, array('id' => 'post_method', 'style' => 'width: 100px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Offer Creative 1</td>
                                        <td align="left" style='border: 1px dotted #ccc; background-color: #efefef;'>
                                            <input type="file" name="data[offer][creative_1]" value="<?php echo ($offers) ? $offers['Offer']['creative_1'] : ""; ?>">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Offer Creative 2</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="file" name="data[offer][creative_2]" value="<?php echo ($offers) ? $offers['Offer']['creative_2'] : ""; ?>">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" valign="top">&nbsp;Offer Copy</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                        	<!-- Gets replaced with TinyMCE, remember HTML in a textarea should be encoded -->
                                        	<textarea id="elm1" name="data[offer][offer_copy]" rows="15" cols="80" style="width: 100%"><?php echo $offers['Offer']['offer_copy']; ?></textarea>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Category</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $category = $offers['Offer']['category'];
                                             } else {
                                                $category = null;
                                             }
                                             echo $form->select('category', $ocategory, $category, array('id' => 'ocategory', 'style' => 'width: 100px')); ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Sub Category</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $scategory = $offers['Offer']['sub_category'];
                                             } else {
                                                $scategory = null;
                                             }
                                             echo $form->select('sub_category', $oscategory, $scategory, array('id' => 'oscategory', 'style' => 'width: 100px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Search keywords</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][keywords]" id="keywords" title="" value="<?php echo ($offers) ? $offers['Offer']['keywords'] : ""; ?>" class="" style="width: 400px">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Country</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $country = $offers['Offer']['target_country'];
                                             } else {
                                                 $country = null;
                                             }
                                             echo $form->select('target_country', $countries, $country, array('id' => 'country', 'style' => 'width: 400px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Offer Type</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                              if ($offers) {
                                                 $offer_type_val = $offers['Offer']['offer_type'];
                                             } else {
                                                $offer_type_val = null;
                                             }
                                             echo $form->select('offer_type', $offer_type, $offer_type_val, array('id' => 'offer_type', 'style' => 'width: 100px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Coupon Code</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][coupon_code]" id="coupon_code" title="" value="<?php echo ($offers) ? $offers['Offer']['coupon_code'] : ""; ?>" class="" style="width: 85px">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Create Date</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][date_added]" id="create_date" value="<?php echo ($offers) ? date('m-d-Y', strtotime($offers['Offer']['date_added'])) : ""; ?>" style="width: 100px;" /><button type="reset" id="t_trigger_a">...</button>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;End Date</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][date_ended]" id="end_date" value="<?php echo ($offers) ? date('m-d-Y', strtotime($offers['Offer']['date_ended'])) : ""; ?>" style="width: 100px;" /><button type="reset" id="t_trigger_b">...</button>
                                        </td>
            						</tr>
                                    <tr>
                                        <td colspan="2">&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td align="left"><input type="submit" value="Save" style="width: 150px;" class="button"></td>
                                    </tr>
                                </table>
                                <script type="text/javascript">
                                    Calendar.setup({
                                        inputField     :    "create_date",      // id of the input field
                                        ifFormat       :    "%m/%d/%Y",       // format of the input field
                                        showsTime      :    false,            // will display a time selector
                                        button         :    "t_trigger_a",   // trigger for the calendar (button ID)
                                        singleClick    :    false,           // double-click mode
                                        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
                                    });
                                    Calendar.setup({
                                        inputField     :    "end_date",      // id of the input field
                                        ifFormat       :    "%m/%d/%Y",       // format of the input field
                                        showsTime      :    false,            // will display a time selector
                                        button         :    "t_trigger_b",   // trigger for the calendar (button ID)
                                        singleClick    :    false,           // double-click mode
                                        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
                                    });
                                </script>
                            </td>
                            <td width="40%" valign="top">
                                <!-- form fields -->
                                <table class="adminlist" cellpadding=20>
                                    <tr>
                                        <td align="left" width="20%">&nbsp;Form name</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[custom][form_name]" id="form_name" value="<?=($fform)?$fform['Field']['form_name']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" width="20%">&nbsp;Offer Post URL</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[custom][form_action]" id="form_action" value="<?=($fform)?$fform['Field']['form_action']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                </table>
                                <br/>
                                <table class="adminlist" cellpadding=20>
                                    <tr>
                                        <th colspan="2">Form Details</th>
                                    </tr>
                                    <tr>
                                        <td align="left" width="20%">&nbsp;Field Type</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($_fieldd) {
                                                $fftype_val = $_fieldd['Field']['field_type'];
                                             } else {
                                                $fftype_val = "";
                                             }
                                             echo $form->select('fftype', $fftype, $fftype_val, array('id' => 'fftype', 'style' => 'width: 250px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Field label</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="field_label" id="field_label" value="<?=($_fieldd['Field'])?$_fieldd['Field']['field_label']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Field name</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="field_name" id="field_name" value="<?=($_fieldd['Field'])?$_fieldd['Field']['field_name']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" valign="top">&nbsp;Field value</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="field_value" id="field_value" value="<?=($_fieldd['Field'])?$_fieldd['Field']['field_value']:""?>" style="width: 245px;" />
                                            <p style="font-size: 11px; color: red; font-style: italic;">For drop down value, use comma to separate options</p>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Required</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($_fieldd) {
                                                if ($_fieldd['Field']['required']) {
                                                    $checked = true;
                                                } else {
                                                    $checked = false;
                                                }
                                             } else {
                                                $checked = false;
                                             }
                                             ?>
                                            <input type="checkbox" name="data[custom][required]" id="is_required" checked=<?=$checked?> />
                                        </td>
            						</tr>
            						<tr>
                                        <td align="left">&nbsp;</td>
                                        <td><input type="button" value="Add" style="width: 70px;" class="button" onclick="javascript: checkElementName(document.getElementById('field_name').value);"></td>
                                    </tr>
                                </table>

                                <table class="adminform">
                                    <tr>
                                        <th>Form Fields Preview</th>
                                        <th align="right">
                                            <?php
                                             if ($fields) {
                                                echo $html->link('Export', '/admin/export_form_fields/'.$offerId, array('target' => '_blank'));
                                             }
                                             ?>
                                        </th>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <div id="preview"></div>
                                            <?php
                                             if ($fields) {
                                             ?>
                                            <div id="preview_edit">
                                                <form name="<?=$fform['Field']['form_name']?>" action="<?=$fform['Field']['form_action']?>" method="<?=($offers['Offer']['post_method']==0)?'post':'get'?>">
                                                <table cellpadding="20px" class="adminlist">
                                            <?php
                                                 foreach ($fields as $v) {
                                                     if ($v['Field']['field_type']) {
                                             ?>
                                                    <tr>
                                                        <td width="20%"><?php echo $v['Field']['field_label']; ?></td>
                                                        <td width="%">
                                                        <?php
                                                         switch($v['Field']['field_type']){
                                                            case "select":
                                                                $options = explode(",", str_replace(", ", ",", $v['Field']['field_value']));
                                                         ?>
                                                            <select id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>">
                                                        <?php
                                                         foreach ($options as $opt) {
                                                         ?>
                                                                <option value="<?php echo $opt?>"><?php echo $opt?></option>
                                                        <?php
                                                         }
                                                         ?>
                                                            </select>
                                                        <?php
                                                                break;

                                                            default:
                                                         ?>
                                                            <input type="<?php echo $v['Field']['field_type']?>" id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" value="<?php echo ($v['field_value'])?$v['field_value']:"";?>">
                                                        <?php
                                                                break;
                                                         } // end switch
                                                         ?>
                                                        </td>
                                                        <td width="10%">
                                                            <a href="<?php echo $html->url('/admin/add_offers/'.$offerId.'/f/edit/'.$v['Field']['id']); ?>"><?php echo $html->image('admin/tick.png', array('border' => 0, 'alt' => 'Edit')); ?></a>&nbsp;&nbsp;
                                                            <a href="<?php echo $html->url('/admin/add_offers/'.$offerId.'/f/delete/'.$v['Field']['id']); ?>"><?php echo $html->image('admin/publish_x.png', array('border' => 0, 'alt' => 'Delete')); ?></a>
                                                        </td>
                                                    </tr>
                                            <?php
                                                     }
                                                 }
                                             ?>
                                                </table>

                                                <input type="hidden" id="edit_field_id" name="data[field][edit_field_id]" value="<?=$edit_field_id?>">
                                                </form>
                                            </div>
                                            <?php
                                             }
                                             ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>

                    <input type="hidden" name="data[offer][manage]" id="offer" />
                    <input type="hidden" id="flag" name="data[window][flag]">
                    </form>
                </td>
            </tr>
        </table>

    </div>
</div>

<script type="text/javascript">
<!--
function checkElementName(val)
{
    var cn = document.getElementById("preview").childNodes;
    var fn = document.getElementById("field_name").value;
    var fv = document.getElementById("field_value").value;
    var proceed = true;

    if (fn == "" || fv == "") {
        alert("Incomplete details");
        return false;
    }

    for (var i = 0; i < cn.length; i++) {
        if ( cn[i].nodeType==1 ) {
            if (cn[i].value == val) {
                proceed = false;
                alert("field name already exists");
                return false;
            } else {
                proceed = true;
            }
        } else {
            proceed = true;
        }
    }

    if (proceed) {
        createCustomElement();
    }
}
function createCustomElement()
{
    var field_type = document.getElementById('fftype');
    var field_label = document.getElementById('field_label').value;
    var field_name = document.getElementById('field_name').value;
    var field_value = document.getElementById('field_value').value;
    var required = document.getElementById('is_required').checked;

    if (field_type.value) {
        var element;
        switch (field_type.value) {
            case "text":
                element = "<label>"+field_label+"</label>&nbsp;";
                element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                element += "<input type='hidden' name='data[custom][field_label-"+field_label+"]' value='"+field_label+"'>";
                element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                break;

            case "hidden":
                element = "<label>"+field_label+"</label>&nbsp;";
                element += "<input type='hidden' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_label-"+field_label+"]' value='"+field_label+"'>";
                element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                break;

            case "checkbox":
                element = "<label>"+field_label+"</label>&nbsp;";
                element += "<input type='checkbox' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_label-"+field_label+"]' value='"+field_label+"'>";
                element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                break;

            case "select":
                var _fv = field_value.replace(" ", "");
                var fv = field_value.split(',');
                var option;

                for (i=0; i < fv.length; i++) {
                    if (fv[i]) {
                        option += "<option value='"+fv[i]+"'>"+fv[i]+"</option>";
                    }

                }

                element = "<label>"+field_label+"</label>&nbsp;";
                element += "<select name='"+field_name+"'>";
                element += option;
                element += "</select>";
                element += "<input type='hidden' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_label-"+field_label+"]' value='"+field_label+"'>";
                element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                break;
        }
    }

    var current = document.getElementById('preview').innerHTML;
    var preview = document.getElementById('preview');

    preview.innerHTML = current + '<br/>' + element;
    document.getElementById('edit_field_id').value = "";

}

function submitthis()
{
    var flag = document.getElementById('flag').value;
    switch (flag) {
        case "offer":
            document.getElementById('offer').value = 'manage';
            document.businessForm.submit();
            break;

        /*case "disable":
            var msg = "Are you sure you want to disable the selected advertiser?";
            if (confirm(msg)) {
                document.businessForm.submit();
            }
            break;

        case "delete":
            var msg = "Are you sure you want to delete the selected advertiser?";
            if (confirm(msg)) {
                document.businessForm.submit();
            }
            break;*/

        default:
            document.businessForm.submit();
            break;
    }
}
-->
</script>